package com.hfzy.ihk.web.aliyuncall.biz;

import com.hfzy.ihk.common.util.ValidateUtils;
import com.hfzy.ihk.common.util.date.DateUtils;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.facade.aliyuncall.enums.MainCallTypeEnum;
import com.hfzy.ihk.web.aliyuncall.constant.Constants;
import com.hfzy.ihk.web.aliyuncall.constant.RedisFields;
import com.hfzy.ihk.web.aliyuncall.constant.RedisKey;
import com.hfzy.ihk.web.aliyuncall.constant.RedisTimeOut;
import com.hfzy.ihk.web.aliyuncall.vo.AliyunTransNumVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by YANFA on 2018/10/17.
 */
@Component
public class AliyunTransNumBiz {

    @Autowired
    RedisTemplate redisTemplate;

    public String AliyunTransNum(AliyunTransNumVo vo) throws Exception{

        String targetCallPhone = null;
        Map<String,String> redisSaveMap = new HashMap<>();
        String  nowDateStr = DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT_DATETIME_SSS);


        String mainCallPhone = vo.getCaller_num();

        //判断如果不是手机并且不是以0开头就加0
        if(!ValidateUtils.isMobile(mainCallPhone)){
            if (!mainCallPhone.startsWith("0")){
                mainCallPhone = "0"+mainCallPhone;
            }
        }

        //这里应该有一个获取被叫的逻辑

        targetCallPhone="13418035053"; //写死

        redisSaveMap.put(RedisFields.CALL_HISTORY_MESSAGE_ID,vo.getCall_id());
        redisSaveMap.put(RedisFields.CALL_HISTORY_SESSION_ID, vo.getSessionId());
        redisSaveMap.put(RedisFields.CALL_HISTORY_MAIN_CALL_PHONE, mainCallPhone);
        redisSaveMap.put(RedisFields.CALL_HISTORY_TARGET_CALL_PHONE, targetCallPhone);
        redisSaveMap.put(RedisFields.CALL_HISTORY_SHOW_NUM, vo.getCaller_show_num());
        redisSaveMap.put(RedisFields.CALL_HISTORY_IMS, vo.getCalled_show_num());
        redisSaveMap.put(RedisFields.CALL_HISTORY_MAIN_CALL_ANSWERED, Constants.TRUE);
        redisSaveMap.put(RedisFields.CALL_HISTORY_MAIN_CALL_ANSWERED_TIME, nowDateStr);
        redisSaveMap.put(RedisFields.RECEIVE_CALL_HISTORY_MAIN_CALL_ANSWERED, nowDateStr);
        redisSaveMap.put(RedisFields.CALL_HISTORY_DATA_ID, "-1");
        redisSaveMap.put(RedisFields.CALL_HISTORY_CITY, "-1"); //目前无法区分是哪个城市打来
        redisSaveMap.put(RedisFields.CALL_HISTORY_CALL_TYPE, Constants.IN_COME.toString());
        redisSaveMap.put(RedisFields.CALL_HISTORY_MAIN_CALL_TYPE, String.valueOf(MainCallTypeEnum.MAIN_CALL_PHONE_IS_CUSTOMER.getValue()));
        redisSaveMap.put(RedisFields.CREATE_TIME, nowDateStr);
        redisSaveMap.put(RedisFields.CREATE_USER_ID, "-1");
        redisSaveMap.put(RedisFields.CALL_HISTORY_COMMIT_TO,"11");

        /*保存呼叫数据,2天有效*/
        redisTemplate.opsForHash().putAll(RedisKey.CALL_HISTORY_RPIX + vo.getSessionId(), redisSaveMap);
        //超时时间三天
        redisTemplate.expire(RedisKey.CALL_HISTORY_RPIX + vo.getSessionId(), RedisTimeOut.CACHE_THREE_DAY, TimeUnit.SECONDS);

        if(!ValidateUtils.isMobile(targetCallPhone)){
            if (!targetCallPhone.startsWith("0")){
                targetCallPhone = "020"+targetCallPhone;
            }
        }
        return targetCallPhone;
    }

}
